package com.sunrise.visual.frame.common
{
	public class HbarUtil
	{
		private var data:Array;
		private	var config:Object;
		
		private var plot_type:String;	//柱图显示方式，CategorizedVertical：竖直方向	CategorizedHorizontal:水平方向
		private var dataLabelEnabled:String;	//是否在柱图上显示数据，true or false
		private var dataLabelPosition:String;	//柱图上显示数据位置
		private var format_data:String;	//保留小数位
		private var unit:String;	//单位
		private var wdbm:String;	//维度编码
		private var zbbm:Array;	//指标编码
		private var zbmc:Array;	//指标名称
		private var titleEnabled:String;	//是否启用标题
		private var title:String;	//启用标题时，标题名称
		private var seriesColor:Array;	//柱图颜色
		private var legendEnabled:String;	//是否启用图例
		private var controlsLabelEnabled:String;	//是否显示单位标签
		private var controlsLabelText:String;	//单位标签文本
		
		public function HbarUtil(data:Array, config:Object)
		{
			this.data = data;
			this.config = config;
		}
		
		public function getXML():XML {
			var xml:XML = <anychart>
							<margin left="0" right="0" top="0" bottom="0"/>
							<settings>
								<animation enabled="true"/>
							</settings>
							<charts>
								<chart plot_type={this.plot_type}>
									<data_plot_settings default_series_type="Bar">
										<bar_series style="Silver" point_padding="0.2" group_padding="1">
											<label_settings enabled={this.dataLabelEnabled}>
												<background enabled="false"/>
<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
												<format>{this.config.labelFormat ||"{%YValue}{numDecimals:"}{this.format_data}{"}"}</format>
												<effects>
													<drop_shadow enabled="true" opacity="1"/>
												</effects>
												<position anchor={this.dataLabelPosition}/>
											</label_settings>
											<tooltip_settings enabled="True">
<font family="Microsoft Yahei" bold="false" size="12"/>
												<format>{this.config.tooltipFormat ||"{%Name}\n{%SeriesName}:{%YValue}{numDecimals:"}{this.format_data}{this.config.endLabel || "}"}{this.unit}</format>
											</tooltip_settings>
										</bar_series>
									</data_plot_settings>
									<chart_settings>
										<chart_background enabled="false"/>
										<data_plot_background enabled="false"/>
										<title enabled={this.titleEnabled}>
											<text>{this.title}</text>
										</title>
										<controls>
											<label position="Fixed" anchor="RightTop" inside_dataplot="false" enabled={this.controlsLabelEnabled}>
<font family="Microsoft Yahei" bold="False" size="12" color="white" />
												<text>{this.controlsLabelText}</text>
											</label>
										</controls>
										<axes>
											<x_axis>
												<title enabled="false"/>
												<labels>
<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
													</labels>
												<line color="white"/>
												<major_tickmark color="white"/>
												<major_grid enabled="false"/>
												<minor_grid enabled="false"/>
											</x_axis>
											<y_axis position="Opposite">
<scale mode="Normal" maximum_offset={this.config.maximumOffset||"0.1"}/>
												<title enabled="false"/>
												<labels>
<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
													<format>{"{%Value}{numDecimals:"}{this.format_data}{"}"}</format>
												</labels>
												<line color="white"/>
												<major_tickmark color="white"/>
												<major_grid enabled="false"/>
												<minor_grid enabled="false"/>
											</y_axis>
										</axes>
										<legend enabled={this.legendEnabled} ignore_auto_item="true" align="center"	position="right" rows_padding="1" columns_padding="1">
<font family="Microsoft Yahei" bold="False" size="12" color="white"/>
											<background enabled="false"/>
											<title enabled="false"/>
											<items>
												<item source="series"/>
											</items>
										</legend>
									</chart_settings>
									<data>
										
									</data>
								</chart>
							</charts>
							<settings>
								<no_data show_waiting_animation="false">
									<label>
										<text>没有数据</text>
									</label>
								</no_data>
							</settings>
						</anychart>;
			return xml;
		}
		
		public function parseXML():XML {
			this.plot_type = this.config.plot_type || "CategorizedHorizontal";
			this.dataLabelEnabled = this.config.dataLabelEnabled || "true";
			this.dataLabelPosition = this.config.dataLabelPosition || "Center";
			this.format_data = this.config.format_data || "0";
			this.unit = this.config.unit || "";
			this.wdbm = this.config.wdbm;
			this.zbbm = this.config.zbbm;
			this.zbmc = this.config.zbmc;
			this.titleEnabled = this.config.titleEnabled || "false";
			this.title = this.config.title || "";
			this.seriesColor = this.config.seriesColor || ['#00699C','#D2B993','#6B8E23'];
			this.legendEnabled = this.config.legendEnabled || "false";
			this.controlsLabelEnabled = this.config.controlsLabelEnabled || "false";
			this.controlsLabelText = this.config.controlsLabelText || "";
			
			var barXml:XML = this.getXML();
			var xmlStr:String = "";
			barXml.charts[0].chart[0].data[0]="";
			for(var i:int = 0; i < this.zbbm.length; i++){
				xmlStr = '<series name="' + this.zbmc[i] + '" type="Bar" color="' + this.seriesColor[i] + '">'; 
				for(var j:int = 0; j < this.data.length; j++){
					xmlStr += '<point name="' + this.data[j][wdbm] + '" y="' + this.data[j][zbbm[i]]+'" color="'+(this.data[j]['color'])+'"/>';
				}
				xmlStr+='</series>';
				barXml.charts[0].chart[0].data[0].appendChild(new XML(xmlStr));
			}
			return barXml;
		}
	}
}